package com.qdxwx.common.utils;

import jakarta.servlet.http.HttpServletRequest;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public enum TKClientOs {
    WINDOWS_10("Windows 10", new String[]{"Windows NT 6.4", "Windows NT 10"}, null),
    WINDOWS_81("Windows 8.1", new String[]{"Windows NT 6.3"}, null),
    WINDOWS_8("Windows 8", new String[]{"Windows NT 6.2"}, new String[]{"Xbox", "Xbox One"}),
    WINDOWS_7("Windows 7", new String[]{"Windows NT 6.1"}, new String[]{"Xbox", "Xbox One"}),
    WINDOWS_VISTA("Windows Vista", new String[]{"Windows NT 6"}, new String[]{"Xbox", "Xbox One"}),
    WINDOWS_2000("Windows 2000", new String[]{"Windows NT 5.0"}, null),
    WINDOWS_XP("Windows XP", new String[]{"Windows NT 5"}, null),
    WINDOWS_10_MOBILE("Windows 10 Mobile", new String[]{"Windows Phone 10"}, null),
    WINDOWS_PHONE8_1("Windows Phone 8.1", new String[]{"Windows Phone 8.1"}, null),
    WINDOWS_PHONE8("Windows Phone 8", new String[]{"Windows Phone 8"}, null),
    WINDOWS_MOBILE7("Windows Phone 7", new String[]{"Windows Phone OS 7"}, null),
    WINDOWS_MOBILE("Windows Mobile", new String[]{"Windows CE"}, null),
    WINDOWS_98("Windows 98", new String[]{"Windows 98", "Win98"}, new String[]{"Palm"}),
    WINDOWS("Windows", new String[]{"Windows"}, new String[]{"Palm"}),
    XBOX_OS("Xbox OS", new String[]{"xbox"}, new String[0]),
    ANDROID6("Android 6.x", new String[]{"Android 6", "Android-6"}, new String[]{"glass"}),
    ANDROID6_TABLET("Android 6.x Tablet", new String[]{"Android 6", "Android-6"}, new String[]{"mobile", "glass"}),
    ANDROID5("Android 5.x", new String[]{"Android 5", "Android-5"}, new String[]{"glass"}),
    ANDROID5_TABLET("Android 5.x Tablet", new String[]{"Android 5", "Android-5"}, new String[]{"mobile", "glass"}),
    ANDROID4("Android 4.x", new String[]{"Android 4", "Android-4"}, new String[]{"glass", "ubuntu"}),
    ANDROID4_TABLET("Android 4.x Tablet", new String[]{"Android 4", "Android-4"}, new String[]{"mobile", "glass", "ubuntu"}),
    ANDROID4_WEARABLE("Android 4.x", new String[]{"Android 4"}, new String[]{"ubuntu"}),
    ANDROID3_TABLET("Android 3.x Tablet", new String[]{"Android 3"}, null),
    ANDROID2("Android 2.x", new String[]{"Android 2"}, null),
    ANDROID2_TABLET("Android 2.x Tablet", new String[]{"Kindle Fire", "GT-P1000", "SCH-I800"}, null),
    ANDROID1("Android 1.x", new String[]{"Android 1"}, null),
    ANDROID_MOBILE("Android Mobile", new String[]{"Mobile"}, new String[]{"ubuntu"}),
    ANDROID_TABLET("Android Tablet", new String[]{"Tablet"}, null),
    ANDROID("Android", new String[]{"Android"}, new String[]{"Ubuntu"}),
    CHROME_OS("Chrome OS", new String[]{"CrOS"}, null),
    WEBOS("WebOS", new String[]{"webOS"}, null),
    PALM("PalmOS", new String[]{"Palm"}, null),
    MEEGO("MeeGo", new String[]{"MeeGo"}, null),
    iOS9_IPHONE("iOS 9 (iPhone)", new String[]{"iPhone OS 9"}, null),
    iOS8_4_IPHONE("iOS 8.4 (iPhone)", new String[]{"iPhone OS 8_4"}, null),
    iOS8_3_IPHONE("iOS 8.3 (iPhone)", new String[]{"iPhone OS 8_3"}, null),
    iOS8_2_IPHONE("iOS 8.2 (iPhone)", new String[]{"iPhone OS 8_2"}, null),
    iOS8_1_IPHONE("iOS 8.1 (iPhone)", new String[]{"iPhone OS 8_1"}, null),
    iOS8_IPHONE("iOS 8 (iPhone)", new String[]{"iPhone OS 8"}, null),
    iOS7_IPHONE("iOS 7 (iPhone)", new String[]{"iPhone OS 7"}, null),
    iOS6_IPHONE("iOS 6 (iPhone)", new String[]{"iPhone OS 6"}, null),
    iOS5_IPHONE("iOS 5 (iPhone)", new String[]{"iPhone OS 5"}, null),
    iOS4_IPHONE("iOS 4 (iPhone)", new String[]{"iPhone OS 4"}, null),
    MAC_OS_X_IPAD("Mac OS X (iPad)", new String[]{"iPad"}, null),
    iOS9_IPAD("iOS 9 (iPad)", new String[]{"OS 9"}, null),
    iOS8_4_IPAD("iOS 8.4 (iPad)", new String[]{"OS 8_4"}, null),
    iOS8_3_IPAD("iOS 8.3 (iPad)", new String[]{"OS 8_3"}, null),
    iOS8_2_IPAD("iOS 8.2 (iPad)", new String[]{"OS 8_2"}, null),
    iOS8_1_IPAD("iOS 8.1 (iPad)", new String[]{"OS 8_1"}, null),
    iOS8_IPAD("iOS 8 (iPad)", new String[]{"OS 8_0"}, null),
    iOS7_IPAD("iOS 7 (iPad)", new String[]{"OS 7"}, null),
    iOS6_IPAD("iOS 6 (iPad)", new String[]{"OS 6"}, null),
    iOS("iOS", new String[]{"iPhone OS", "like Mac OS X"}, null),
    MAC_OS_X_IPHONE("Mac OS X (iPhone)", new String[]{"iPhone"}, null),
    MAC_OS_X_IPOD("Mac OS X (iPod)", new String[]{"iPod"}, null),
    MAC_OS_X("Mac OS X", new String[]{"Mac OS X", "CFNetwork"}, null),
    MAC_OS("Mac OS", new String[]{"Mac"}, null),
    MAEMO("Maemo", new String[]{"Maemo"}, null),
    BADA("Bada", new String[]{"Bada"}, null),
    GOOGLE_TV("Android (Google TV)", new String[]{"GoogleTV"}, null),
    KINDLE("Linux (Kindle)", new String[]{"Kindle"}, null),
    KINDLE3("Linux (Kindle 3)", new String[]{"Kindle/3"}, null),
    KINDLE2("Linux (Kindle 2)", new String[]{"Kindle/2"}, null),
    LINUX("Linux", new String[]{"Linux", "CamelHttpStream"}, null),
    UBUNTU("Ubuntu", new String[]{"ubuntu"}, null),
    UBUNTU_TOUCH_MOBILE("Ubuntu Touch (mobile)", new String[]{"mobile"}, null),
    SYMBIAN9("Symbian OS 9.x", new String[]{"SymbianOS/9", "Series60/3"}, null),
    SYMBIAN8("Symbian OS 8.x", new String[]{"SymbianOS/8", "Series60/2.6", "Series60/2.8"}, null),
    SYMBIAN7("Symbian OS 7.x", new String[]{"SymbianOS/7"}, null),
    SYMBIAN6("Symbian OS 6.x", new String[]{"SymbianOS/6"}, null),
    SERIES40("Series 40", new String[]{"Nokia6300"}, null),
    SYMBIAN("Symbian OS", new String[]{"Symbian", "Series60"}, null),
    SONY_ERICSSON("Sony Ericsson", new String[]{"SonyEricsson"}, null),
    SUN_OS("SunOS", new String[]{"SunOS"}, null),
    PSP("Sony Playstation", new String[]{"Playstation"}, null),
    WII("Nintendo Wii", new String[]{"Wii"}, null),
    BLACKBERRY("BlackBerryOS", new String[]{"BlackBerry"}, null),
    BLACKBERRY7("BlackBerry 7", new String[]{"Version/7"}, null),
    BLACKBERRY6("BlackBerry 6", new String[]{"Version/6"}, null),
    BLACKBERRY_TABLET("BlackBerry Tablet OS", new String[]{"RIM Tablet OS"}, null),
    ROKU("Roku OS", new String[]{"Roku"}, null),
    PROXY("Proxy", null, null),
    MOBILE("mobile", new String[]{"Mobile"}, null),
    UNKNOWN("Unknown", new String[0], null);

    private final String name;
    private final String[] aliases;
    private final String[] excludeList;

    TKClientOs(String name, String[] aliases, String[] exclude) {
        this.name = name;
        this.aliases = aliases;
        this.excludeList = exclude;
    }

    public String getName() {
        return this.name;
    }

    public static String getName(String agentString) {

        return TKClientOs.parse(agentString).getName();
    }

    public static String getName(HttpServletRequest request) {

        return TKClientOs.parse(request).getName();
    }

    private boolean containsAliases(String agentString) {
        return contains(agentString, this.aliases);
    }

    private boolean containsExclude(String agentString) {
        return contains(agentString, this.excludeList);
    }

    private boolean checkUserAgentString(String agentString) {
        if (this.containsAliases(agentString)) {
            return !this.containsExclude(agentString);
        }
        return false;
    }

    public static TKClientOs parse(String agentString) {
        if (agentString != null) {
            agentString = agentString.toLowerCase();
            for (TKClientOs match : TKClientOs.values()) {
                if (match.checkUserAgentString(agentString)) {
                    return match;
                }
            }
        }
        return UNKNOWN;
    }

    public static TKClientOs parse(HttpServletRequest request) {
        return parse(request.getHeader("User-Agent"));
    }

    boolean contains(String str, String[] inArr) {
        if (inArr != null) {
            for (String inStr : inArr) {
                if (str.contains(inStr.toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }
}
